package com.hbx.platform.org.controller;

import com.hbx.platform.base.annotation.AutoPermission;
import com.hbx.platform.org.domain.Department;
import com.hbx.platform.org.query.DepartmentQuery;
import com.hbx.platform.org.service.IDepartmentService;
import com.hbx.platform.base.utils.AjaxResult;
import com.hbx.platform.base.utils.PageList;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: hbx
 * @date: 2024/03/29
 **/
@RestController
@RequestMapping("/dept")
@AutoPermission(name = "部门管理")
@Api(value = "部门的API",description = "部门的CRUD")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    @GetMapping
    @ApiOperation(value = "查询所有部门")
    public AjaxResult getAll(){
        try {
            List<Department> list = departmentService.getAll();
            return AjaxResult.me().setData(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("报错："+e.getMessage());
        }
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询部门")
    public AjaxResult queryById(@PathVariable("id") @ApiParam("部门id") Long id){
        Department department = null;
        try {
            department = departmentService.queryById(id);
            return AjaxResult.me().setData(department);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("报错："+e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除部门")
    @AutoPermission(name = "删除部门",desc = "根据id删除单条记录")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            departmentService.delete(id);
            return AjaxResult.me().setMessage("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("报错："+e.getMessage());
        }
    }

    @PutMapping
    @ApiOperation("id为null添加数据，反之修改数据")
    @AutoPermission(name = "添加或修改部门",desc = "id为null添加数据，反之修改数据")
    public AjaxResult saveOrUpdate(@RequestBody Department department){
        try {
            if(department.getId() == null){
                departmentService.save(department);
                return AjaxResult.me().setMessage("添加成功");
            }else{
                departmentService.update(department);
                return AjaxResult.me().setMessage("修改成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("报错："+e.getMessage());
        }
    }

    /**
     * 分页
     * @param query
     * @return
     */
    @PostMapping
    public AjaxResult page(@RequestBody DepartmentQuery query){
        PageList<Department> list = null;
        try {
            list = departmentService.page(query);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询分页失败");
        }
        return AjaxResult.me().setData(list);
    }

    @DeleteMapping("/batch/{ids}")
    @AutoPermission(name = "批量删除部门",desc = "批量删除")
    public AjaxResult batchDelete(@PathVariable("ids") List<Long> ids){
        Integer count = null;
        try {
            count = departmentService.batchDelete(ids);
            return AjaxResult.me().setMessage("删除了"+count+"条记录");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("批量删除失败");
        }
    }

    @GetMapping("/tree")
    public AjaxResult deptTree(){
        try {
            List<Department> tree = departmentService.getTree();
            return AjaxResult.me().setData(tree);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询部门tree失败");
        }
    }

}
